<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="用户昵称" prop="nickName">
        <el-select
          v-model="dataForm.nickName"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账期" prop="monthValue">
        <el-date-picker
          v-model="dataForm.monthValue"
          type="month"
          placeholder="选择月"
          format="yyyyMM"
          value-format="yyyyMM"
          @change="monthChange()"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="基本薪资" prop="base_salary">
        <el-input
          v-model="dataForm.base_salary"
          placeholder="base_salary"
        ></el-input>
      </el-form-item>
      <el-form-item label="补贴" prop="allowance">
        <el-input
          v-model="dataForm.allowance"
          placeholder="allowance"
        ></el-input>
      </el-form-item>
      <el-form-item label="扣除" prop="deduction">
        <el-input
          v-model="dataForm.deduction"
          placeholder="deduction"
        ></el-input>
      </el-form-item>
      <el-form-item label="统计" prop="total_salary">
        <el-input
          v-model="dataForm.total_salary"
          placeholder="total_salary"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["options"],
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        nickName: "",
        monthValue: "",
        base_salary: "",
        allowance: "",
        deduction: "",
        total_salary: "",
      },
      dataRule: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        monthValue: [
          { required: true, message: "账期不能为空", trigger: "blur" },
        ],
        base_salary: [
          { required: true, message: "基本薪资不能为空", trigger: "blur" },
        ],
        allowance: [
          { required: true, message: "补贴不能为空", trigger: "blur" },
        ],
        deduction: [
          { required: true, message: "扣除不能为空", trigger: "blur" },
        ],
        total_salary: [
          { required: true, message: "总计不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id) {
          this.dataForm.id = id.id;
          this.dataForm.monthValue = id.month_id;
          this.dataForm.nickName = id.user_id;
          this.dataForm.base_salary = id.base_salary;
          this.dataForm.allowance = id.allowance;
          this.dataForm.deduction = id.deduction;
          this.dataForm.total_salary = id.total_salary;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/business/esalary/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id,
              userId: this.dataForm.nickName,
              monthId: this.dataForm.monthValue,
              base_salary: this.dataForm.base_salary,
              allowance: this.dataForm.allowance,
              totalSalary: this.dataForm.total_salary,
              deduction: this.dataForm.deduction,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
